<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>登录页面-Quanta</title>
    <link rel="stylesheet" href="css/main.css" type="text/css">
    <script src="js/jquery-3.1.1.min.js"></script>
    <script src="js/common.js" ></script>
    <style>
        html{
            width:100%;
            height:100%;
            margin:0;
            padding:0;
        }
    </style>
</head>
<body style="background-color: lavender;">
    <form class="basic-form" style="width: 450px;margin: auto;">
        <label>
            <span><cite class="red">*</cite>用户名：</span>
            <input placeholder="请输入用户名" name="id" type="text" />
        </label>
        <br>
        <label><span><cite class="red">*</cite>密码：</span><input placeholder="请输入密码" name="pwd" type="text" /></label>
        <br>
            <label style="height: 15px;line-height: 15px;margin: -10px 0px 10px 100px;">记住用户名密码
                <input style="width: auto;height: auto;margin-top: 0px;" type="checkbox" name="remember" />
            </label>
        <br>
        <cite class="errorInfo" style="margin-left: 120px;margin-bottom: 30px;" id="error"></cite>
        <br>
        <br>
        <br>
        <div style="margin: auto;">
            <a onclick="login()" style="margin-left: 120px;">登录</a>
            <a href="register.html" style="margin-left: 40px;color: #000;">跳转注册</a>
        </div>
    </form>
</body>
<script type="text/javascript">
    var login = function () {
        var datas = {},localError = false;
        $('.basic-form [name]').each(function (n,e) {
            if(localError){
                return;
            }
            if(e.name == 'remember'){
                datas[e.name] = e.value?1:0;
            }else{
                if(!e.value && e.name == 'id'){
                    $(e).focus();
                    $('#error').text('用户名不能为空');
                    localError = true;
                    return;
                }
                if(!e.value && e.name == 'pwd'){
                    $(e).focus();
                    $('#error').text('密码不能为空');
                    localError = true;
                    return;
                }
                datas[e.name] = e.value;
            }
        });
        if(!localError){
            $.ajax({
                type:'POST',
                url:cm.params.postUrl+'login',
                data:datas,
                success:function(result){
                    var res = JSON.parse(result);
                    if(res.err){
                        $('#error').text(res.err);
                    }else if(res.token){
                        cm.setToken(res.token);
                        alert('登录成功');
                        window.setTimeout(function () {
                            window.location.href = cm.params.postUrl + cm.params.indexUrl;
                        },1000)
                    }else{
                        $('#error').text('失败了......');
                    }
                },
                error:function (msg) {
                    alert('问题严重了');
                }
            });
        }
    }
</script>
</html>